package com.lineage.server.model.weaponskill;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class W_SK008 extends L1WeaponSkillType {
  private static final Log _log = LogFactory.getLog(W_SK008.class);
  
  private static final Random _random = new Random();
  
  public static L1WeaponSkillType get() {
    return new W_SK008();
  }
  
  public double start_weapon_skill(L1PcInstance pc, L1Character target, L1ItemInstance weapon, double srcdmg) {
    try {
      int chance = _random.nextInt(1000);
      if (random(weapon) + pc.getWeaponSkillChance() * 10 < chance)
        return 0.0D; 
      if (target.hasSkillEffect(31)) {
        target.removeSkillEffect(31);
        int castgfx2 = SkillsTable.get().getTemplate(31).getCastGfx2();
        target.broadcastPacketAll((ServerBasePacket)new S_SkillSound(target.getId(), castgfx2));
        if (target instanceof L1PcInstance) {
          L1PcInstance tgpc = (L1PcInstance)target;
          tgpc.sendPacketsAll((ServerBasePacket)new S_SkillSound(tgpc.getId(), castgfx2));
        } 
        return 0.0D;
      } 
      if (target.hasSkillEffect(153))
        target.removeSkillEffect(153); 
      double damage = 0.0D;
      if (this._power == 1) {
        damage = DmgAcMr.getDamageStr(pc);
      } else if (this._power == 2) {
        damage = DmgAcMr.getDamageDex(pc);
      } else if (this._power == 4) {
        damage = DmgAcMr.getDamageInt(pc);
      } else {
        damage = DmgAcMr.getDamage(pc);
      } 
      if (target.getCurrentHp() - damage < 0.0D && target.getCurrentHp() != 1) {
        damage = (target.getCurrentHp() - 1);
      } else if (target.getCurrentHp() == 1) {
        damage = 0.0D;
      } 
      damage += (this._type1 + _random.nextInt(this._type2));
      double outdmg = dmg1() + dmg2(srcdmg) + dmg3(pc) + damage;
      if (pc.getWeaponSkillDmg() != 1.0D)
        outdmg += outdmg * pc.getWeaponSkillDmg(); 
      if (this._type3 > 0)
        outdmg *= this._type3 / 100.0D; 
      show(pc, target);
      return calc_dmg(pc, target, outdmg, weapon);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return 0.0D;
    } 
  }
}
